package com.feini.ktv.svc.controller.om;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feini.ktv.svc.entity.om.*;
import com.feini.ktv.svc.service.om.*;
import com.feini.ktv.svc.util.SysTimeUtil;
import com.podara.skeleton.controller.SkeletonController;
import com.podara.skeleton.log.annotation.LogLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jiangzr
 * @since 2021-04-13
 */
@LogLogger
@RestController
public class OmOrderController implements SkeletonController {

    @Autowired
    private IOmOrderLineService orderLineService;

    @Autowired
    private IOmOfferService offerService;

    @Autowired
    private IOmBoxService boxService;

    @Autowired
    private IOmPriceService priceService;

    @Autowired
    private IOmItemService itemService;

    @Autowired
    private IOmOrderService orderService;

    @GetMapping("/queryOrderList")
    public List<OmOrder> queryOrderList() {
        QueryWrapper<OmOrder> orderQueryWrapper = new QueryWrapper<OmOrder>();
        orderQueryWrapper.eq("order_status", "0");
        List<OmOrder> sellers = this.orderService.list(orderQueryWrapper);
        return sellers;
    }

    @GetMapping("/queryOrderListById/{orderId}")
    public List<OmOrder> queryOrderListById(@PathVariable("orderId") String orderId) {
        QueryWrapper<OmOrder> orderQueryWrapper = new QueryWrapper<OmOrder>();
        orderQueryWrapper.eq("order_status", "0");
        orderQueryWrapper.eq("order_id", orderId);
        List<OmOrder> sellers = this.orderService.list(orderQueryWrapper);
        return sellers;
    }

    @PostMapping("/postOrderList/{boxId}")
    public boolean postOrderList(@PathVariable("boxId") Long boxId, @RequestBody List<Map> orederDatas) {
        //订单明细存在一单多条业务-om_order_line
        boolean secsuce = false;
        if (!Objects.isNull(orederDatas)) {
            QueryWrapper<OmBox> qryBox = new QueryWrapper<OmBox>();
            qryBox.eq("box_id", boxId);
            List<OmBox> boxList = boxService.list(qryBox);

            long orderId = boxList.get(0).getOrderId();
            String orderLineId = pushOrderLine(orderId);

            List<OmOffer> offers = new ArrayList<>();
            List<OmPrice> prices = new ArrayList<>();
            long totalPrice = 0L;
            for (Map maps : orederDatas) {
                String priceTotal = maps.get("priceTotal").toString();
                totalPrice += Double.valueOf(priceTotal) * 100;
                OmOffer offer = pushOfferInfo(orderId, Long.valueOf(orderLineId), maps);
                offers.add(offer);

                OmPrice price = pushOmPirce(orderId, orderLineId, maps);
                prices.add(price);
            }
            secsuce = offerService.saveBatch(offers);
            if (secsuce) {
                priceService.saveBatch(prices);
            }
            if (secsuce) {
                pushItemPirce(orderId, orderLineId, totalPrice);
            }
        }
        return secsuce;
    }

    private void pushItemPirce(long orderId, String orderLineId, long totalPrice) {
        LocalDateTime dateTime = LocalDateTime.now();

        OmItem item = new OmItem();
        item.setItemName("前台收费");
        item.setOrderId(orderId);
        item.setOrderLineId(Long.valueOf(orderLineId));
        item.setIntactItemType("3");//费用项类型 1:预存,2赠送,3营业费
        item.setActualPrice(totalPrice + "");
        item.setShouldFee(totalPrice + "");
        item.setDeductPrice("");
        item.setDeductType("");
        item.setDataStatus("0");
        item.setOrderStatus("0");
        item.setCreateOpId("jiang");
        item.setCreateOrgId("000000");
        item.setCreateDate(dateTime);
        item.setValidDate(dateTime);
        item.setExpireDate(dateTime.withYear(2050).withMonth(12));
        item.setRemark("测试");
        itemService.save(item);
    }

    private OmOffer pushOfferInfo(long orderId, long orderLineId, Map maps) {
        LocalDateTime dateTime = LocalDateTime.now();
        OmOffer offer = new OmOffer();
        String number = maps.get("offerUtil").toString();
        offer.setOfferInsId(Long.valueOf(maps.get("offerId").toString()));
        offer.setOrderId(orderId);
        offer.setOfferName(maps.get("offerName").toString());
        offer.setOfferType(maps.get("offerType").toString());
        offer.setOfferNumber(number.substring(0, number.indexOf("x")));
        offer.setOrderLineId(orderLineId);
        offer.setAction("0");
        offer.setAfterDoneCode("1");
        offer.setBeforeDoneCode("1");
        offer.setCustId(10000001L);
        offer.setIsBundle("1");
        offer.setDataStatus("0");
        offer.setOrderStatus("0");
        offer.setCreateOpId("jiang");
        offer.setCreateOrgId("000000");
        offer.setCreateDate(dateTime);
        offer.setRemark("测试");
        return offer;
    }

    private OmPrice pushOmPirce(long orderId, String orderLineId, Map maps) {
        LocalDateTime dateTime = LocalDateTime.now();
        String priceTotal = maps.get("priceTotal").toString();
        OmPrice price = new OmPrice();
        String id = SysTimeUtil.getSysDateYYYYMMDDHHMMSS();
        price.setPriceInsKid(Long.valueOf(id));//主键
        price.setPriceInsId(0L);
        price.setOfferInsId(Long.valueOf(maps.get("offerId").toString()));
        price.setOrderId(orderId);
        price.setOrderLineId(Long.valueOf(orderLineId));
        price.setPriceType("1");//定价类型 1营业费,2预存
        price.setPriceAct("1");//支付类型 1收取,2转账,3赠送,4扣减
        price.setPriceFee("0");//优惠价
        price.setPriceUnit("分");//分
        price.setPriceName(maps.get("offerName").toString());
        price.setPriceVal(priceTotal);//价格
        price.setCatType("1");//1直接价格 2动态价格
        price.setDataStatus("0");
        price.setOrderStatus("0");
        price.setCreateOpId("jiang");
        price.setCreateOrgId("000000");
        price.setCreateDate(dateTime);
        price.setRemark("测试");
        return price;
    }

    public String pushOrderLine(long orderId) {
        LocalDateTime dateTime = LocalDateTime.now();
        String orderLineId = SysTimeUtil.getSysDateYYYYMMDDHHMMSS();
        OmOrderLine orderLine = new OmOrderLine();
        orderLine.setOrderLineId(Long.parseLong(orderLineId));
        orderLine.setOrderId(orderId);
        orderLine.setIntactId(201L);
        orderLine.setIntactItemContent("om_order_line_h");
        orderLine.setAccessNum("13974970860");
        orderLine.setDataStatus("0");
        orderLine.setOrderStatus("0");
        orderLine.setCreateOpId("jiang");
        orderLine.setCreateOrgId("000000");
        orderLine.setCreateDate(dateTime);
        orderLine.setValidDate(dateTime);
        orderLine.setExpireDate(dateTime.withYear(2050).withMonth(12));
        orderLine.setRemark("测试");
        this.orderLineService.save(orderLine);
        return orderLineId;
    }
}

